package com.netease.xone.itemview;

import java.util.List;

import protocol.meta.AppImage;
import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import protocol.meta.AppVideo;
import Utils.StackTraceUtil;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityMovieView;
import com.netease.xone.widget.LoadingImageView;


public class RenderInfoImageItem implements IRenderInfoBase, OnClickListener{
    private static final String TAG = RenderInfoImageItem.class.getSimpleName();
    
    private View mRoot;
    private LoadingImageView mImage;
    private ImageView mVideo;

    public RenderInfoImageItem(View v) {
        mRoot = v;
        mImage = (LoadingImageView)v.findViewById(R.id.image);
        mVideo = (ImageView)v.findViewById(R.id.video);
        
        int width = v.getContext().getResources().getDimensionPixelSize(R.dimen.info_size_120);
        mImage.setServerClipSize(width, width);
        mImage.setOnClickListener(this);
        mVideo.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Log.d(TAG, StackTraceUtil.getMethodName());
                String value = null;
                AppVideo video = null;
                if (null != v.getTag() && v.getTag() instanceof AppVideo) {
                    video = (AppVideo)v.getTag();
                    value = video.getMediaUrl();
                    ActivityMovieView.playVideo(v.getContext(), value);
                }
            }
        });
    }

    public void renderView(Context context, String imageUrl, String videoUrl) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        if (TextUtils.isEmpty(imageUrl)) {
            mRoot.setVisibility(View.GONE);
        } else {
            mRoot.setVisibility(View.VISIBLE);
            mImage.setLoadingImage(imageUrl);
        }
        
        if (TextUtils.isEmpty(videoUrl)) {
            mVideo.setVisibility(View.GONE);
        } else {
            mVideo.setVisibility(View.VISIBLE);
            mVideo.setTag(videoUrl);
        }
    }
    
    private void renderView(Context context, List<AppImage> list) {
        String value = null;
        if (null != list && list.size() > 0) {
            value = list.get(0).getUrl();
        }
        renderView(context, value, null);
    }
    
    @Override
    public void renderView(Context context, AppInfo appInfo) {
        renderView(context, appInfo.getImages());
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        renderView(context, appInfoDetail.getImages());
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
    }
}
